package com.wwcom.darteagle.model;

import java.util.ArrayList;
import java.util.List;

//We always have an unfinalised VisitScore on the end of the stack
public class LegPlayer {
	private MatchPlayer matchPlayer;
	private MatchInfo matchInfo;
	private List<VisitScore> visitScores;
	private VisitScore currentVisitScore;
	
	public LegPlayer( MatchInfo matchInfo, MatchPlayer matchPlayer) {	
		initialiseSelf();
		this.matchInfo= matchInfo;
		this.matchPlayer= matchPlayer;
		addVisitScore();
	}
	
	private void initialiseSelf() {
		visitScores= new ArrayList<VisitScore>();
		matchInfo= null;
		matchPlayer= null;
		currentVisitScore= null;
	}
	
	//accessors
	public MatchPlayer getMatchPlayer() {
		return matchPlayer;
	}	
	
	public List<VisitScore> getVisitScores() {
		return visitScores;
	}

	//scoring logic
	public VisitScore getCurrentVisitScore() {		
		return currentVisitScore;
	}	

	private void addVisitScore() {
		if (visitScores.size() > 0)
			currentVisitScore= new VisitScore(matchInfo,matchPlayer,visitScores.get(visitScores.size()-1));
		else
			currentVisitScore= new VisitScore(matchInfo,matchPlayer,null);
		visitScores.add(currentVisitScore);
	}	
	
	public void updateCurrentVisitScore( Score score ) {		
		if (currentVisitScore == null) {
			throw new RuntimeException("PlayerLegHistory::updateCurrentVisitScore - " +
					"updating null visit score");
		}
		if (currentVisitScore.isFinalised()) {
			throw new RuntimeException("PlayerLegHistory::updateCurrentVisitScore - " +
					"updating finalised visit score");
			
		}
		currentVisitScore.setScore(score);
	}
	
	public void updateVisitScore( int visitInd, Score score ) {		
		if (visitScores.size() <= visitInd) {
			throw new RuntimeException("PlayerLegHistory::updateVisitScore - " +
					"visitInd out of range");			
		}
		
		VisitScore vs= visitScores.get(visitInd);
		vs.setScore(score);
	}	
		
	public void finaliseCurrentVisitScore() {
		if (currentVisitScore == null) {
			throw new RuntimeException("PlayerLegHistory::finaliseCurrentVisitScore - " +
					"finalising null visit score");
		}
		currentVisitScore.setFinalised(true);
		addVisitScore();
	}
	
	public void finaliseVisitScore( int visitInd) {		
		if (visitScores.size() <= visitInd) {
			throw new RuntimeException("PlayerLegHistory::finaliseVisitScore - " +
					"visitInd out of range");			
		}
		
		VisitScore vs= visitScores.get(visitInd);
		vs.setFinalised(true);
	}	
	
	public void unFinaliseVisitScore( int visitInd) {		
		if (visitScores.size() <= visitInd) {
			throw new RuntimeException("PlayerLegHistory::unFinaliseVisitScore - " +
					"visitInd out of range");			
		}
		
		VisitScore vs= visitScores.get(visitInd);
		vs.setFinalised(false);
	}		
	
	// return the player's last visit
	public VisitScore getLastFinalisedVisitScore() {
		if (visitScores.size() <= 1)
			throw new RuntimeException("PlayerLegHistory::getLastFinalisedVisitScore - " +
					"no finalised visit scores in the list");
		
		return visitScores.get(visitScores.size()-2);
	}	
	
}
